package com.bfly.mall.advert.service.impl;

import com.bfly.core.base.service.BaseServiceImpl;
import com.bfly.core.enums.SysError;
import com.bfly.core.exception.ServiceResponseException;
import com.bfly.mall.advert.entity.Coupon;
import com.bfly.mall.advert.dto.CouponFormDTO;
import com.bfly.mall.advert.enums.CouponStatus;
import com.bfly.mall.advert.enums.CouponUseRange;
import com.bfly.mall.advert.service.ICouponRangeBindService;
import com.bfly.mall.advert.service.ICouponService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * @author 长沙商脉科技有限公司
 * @date 2020/9/20 13:05
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS)
public class CouponServiceImpl extends BaseServiceImpl<Coupon, Integer> implements ICouponService {

    @Autowired
    private ICouponRangeBindService couponGoodsService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(CouponFormDTO formDTO) {
        if (formDTO.getActivityEnd().before(formDTO.getActivityStart())) {
            throw new ServiceResponseException(SysError.PARAM_ERROR, "优惠卷活动开始时间需小于活动结束时间!");
        }
        if (formDTO.getMinimums().compareTo(formDTO.getDiscount()) < 0) {
            throw new ServiceResponseException(SysError.PARAM_ERROR, "最低消费不能小于折扣金额!");
        }
        if (formDTO.getLimitNum() > formDTO.getTotal()) {
            throw new ServiceResponseException(SysError.PARAM_ERROR, "每人限领数量不能大于优惠卷发放总数!");
        }
        CouponUseRange range = CouponUseRange.getRange(formDTO.getUseRange());
        if (range == null) {
            throw new ServiceResponseException(SysError.PARAM_ERROR, "请选择优惠卷使用范围!");
        }

        Coupon coupon = new Coupon();
        BeanUtils.copyProperties(formDTO, coupon);

        Date now = new Date();
        if (coupon.getActivityStart().after(now)) {
            coupon.setStatus(CouponStatus.NOT_START.getId());
        } else if (coupon.getActivityEnd().before(now)) {
            coupon.setStatus(CouponStatus.OFF_SHELF.getId());
        } else {
            coupon.setStatus(CouponStatus.ON_SHELF.getId());
        }

        coupon.setCreateDate(new Date());
        coupon.setUpdateDate(coupon.getCreateDate());
        super.save(coupon);


        if (coupon.getUseRange() != CouponUseRange.ALL.getId()) {
            for (Integer rangeId : formDTO.getRangeId()) {
                // 优惠卷和商品类型或商品绑定关系
                couponGoodsService.save(coupon.getId(), rangeId);
            }
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void edit(CouponFormDTO formDTO) {
        Coupon coupon = get(formDTO.getId());
        if (coupon == null) {
            throw new ServiceResponseException(SysError.PARAM_ERROR, "不存在的优惠卷信息!");
        }
        if (formDTO.getActivityEnd().before(formDTO.getActivityStart())) {
            throw new ServiceResponseException(SysError.PARAM_ERROR, "优惠卷活动开始时间需小于活动结束时间!");
        }
        if (formDTO.getMinimums().compareTo(formDTO.getDiscount()) < 0) {
            throw new ServiceResponseException(SysError.PARAM_ERROR, "最低消费不能小于折扣金额!");
        }
        BeanUtils.copyProperties(coupon, formDTO);

        coupon.setUpdateDate(new Date());
        super.edit(coupon);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void editStatus(int couponId, CouponStatus status) {
        Coupon coupon = get(couponId);
        if (coupon == null) {
            throw new ServiceResponseException(SysError.PARAM_ERROR, "不存在的优惠卷信息!");
        }
        if(status==CouponStatus.ON_SHELF && coupon.getActivityEnd().before(new Date())){
            // 对上架的优惠卷做日期的校验 超出结束日期设置为已过期
            status= CouponStatus.EXPIRED;
        }
        coupon.setUpdateDate(new Date());
        coupon.setStatus(status.getId());
        super.edit(coupon);
    }

    @Override
    @Transactional
    public int remove(Integer... ids) {
        for (Integer id : ids) {
            // 删除优惠卷和商品之间的绑定关系
            couponGoodsService.removeByCoupon(id);
        }
        return super.remove(ids);
    }
}
